/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.utfpr.cm.tsi;

import br.edu.utfpr.cm.TSI.projetoPSA.Facades.AcessoFacade;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Marcelo
 */
public class Login extends HttpServlet {

    private final String PAGINA_LOGIN = "logon.jsp";
    private final String PAGINA_PRINCIPAL = "index.jsp";
    private final String LOGIN = "login";
    private final String SENHA = "senha";
    private final String MENSAGEM_ERRO = "mensagemDeErro";
    private String proximaPagina;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        HttpSession sessao = request.getSession(true);
        try {
            efetuarAutenticacao(request, sessao);
            System.out.println("Redirecionando para: " + PAGINA_PRINCIPAL );
            response.sendRedirect(PAGINA_PRINCIPAL);

        } catch (AutenticacaoException e) {
            request.setAttribute(MENSAGEM_ERRO, e.getMessage());

            RequestDispatcher rd = request.getRequestDispatcher(PAGINA_LOGIN);
            rd.forward(request, response);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void efetuarAutenticacao(HttpServletRequest request, HttpSession sessao) throws AutenticacaoException {
        String login = request.getParameter(LOGIN);
        String senha = request.getParameter(SENHA);
        proximaPagina = request.getParameter("next");

        System.out.println(proximaPagina);

        if ("".equals(login)) {
            throw new AutenticacaoException("Informar o login!");
        }
        if ("".equals(senha)) {
            throw new AutenticacaoException("Informar o senha!");
        }

        if(new AcessoFacade().autenticar(login, senha)){
            sessao.setAttribute("usuarioLogado", new AcessoFacade().getAcessoPorLogin(login).getUsuario());
        }else{
            throw new AutenticacaoException("Usuario ou senha invalida!");
        }        
    }
}
